package com.lcm.jnode.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.render.ViewType;
import com.lcm.jnode.controller.BlogController;
import com.lcm.jnode.controller.IndexController;
import com.lcm.jnode.controller.LaboratoryController;
import com.lcm.jnode.controller.LifeController;
import com.lcm.jnode.controller.TagsController;
import com.lcm.jnode.controller.admin.AdminBlogController;
import com.lcm.jnode.controller.admin.AdminController;
import com.lcm.jnode.controller.admin.AdminLinksController;
import com.lcm.jnode.controller.admin.AdminOptionsController;
import com.lcm.jnode.controller.admin.AdminTagsController;
import com.lcm.jnode.controller.admin.AdminUserController;
import com.lcm.jnode.controller.api.BaiduLogin;
import com.lcm.jnode.controller.api.QQLogin;
import com.lcm.jnode.controller.api.SinaLogin;
import com.lcm.jnode.controller.api.WeChat;
import com.lcm.jnode.handler.ComboHandler;
import com.lcm.jnode.handler.HttpCacheHandler;
import com.lcm.jnode.interceptor.CookieLoginInterceptor;
import com.lcm.jnode.interceptor.OptionsInterceptor;
import com.lcm.jnode.interceptor.SeoInterceptor;
import com.lcm.jnode.interceptor.SessionInterceptor;
import com.lcm.jnode.model.Blog;
import com.lcm.jnode.model.Links;
import com.lcm.jnode.model.MailVerify;
import com.lcm.jnode.model.Options;
import com.lcm.jnode.model.User;
import com.lcm.jnode.model.WBLogin;
import com.lcm.jnode.model.WxRule;
import com.lcm.jnode.utils.ConfigUtil;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

/**
 * config
 * @author L.cm
 * @date 2013-5-29 下午9:12:23
 */
public class JFWebConfig extends JFinalConfig {

	/**
	 * 常量配置
	 */
	@Override
	public void configConstant(Constants me) {
		// 加载配置文件 静态到hashmap中
		ConfigUtil.loadConfig(loadPropertyFile("config.properties"));
		me.setDevMode(getPropertyToBoolean("devMode"));
		me.setViewType(ViewType.JADE);
		me.setBaseViewPath("/WEB-INF/pages");
		me.setError404View("/WEB-INF/pages/error/404.jade");
		me.setError500View("/WEB-INF/pages/error/500.jade");
	}

	/**
	 * 路由配置
	 */
	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/blog", BlogController.class, "/");
		me.add("/tags", TagsController.class, "/tags");
		me.add("/life", LifeController.class, "/life");
		me.add("/laboratory", LaboratoryController.class, "/laboratory");
		// 第三方登录
		me.add("/api/qq", QQLogin.class, "admin");
		me.add("/api/sina", SinaLogin.class, "admin");
		me.add("/api/baidu", BaiduLogin.class, "admin");
		me.add("/api/wechat", WeChat.class, "admin");
		// 后台管理
		me.add("/admin", AdminController.class, "/admin");
		me.add("/admin_blog", AdminBlogController.class, "/admin/blog");
		me.add("/admin_tags", AdminTagsController.class);
		me.add("/admin_user", AdminUserController.class, "/admin/user");
		me.add("/admin_options", AdminOptionsController.class, "/admin");
		me.add("/admin_links", AdminLinksController.class, "/admin/links");
	}

	/**
	 * 全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
	    me.add(new CookieLoginInterceptor());
	    me.add(new SessionInterceptor());
	    me.add(new OptionsInterceptor());
	    me.add(new SeoInterceptor());
	}

	/**
	 * 配置处理器
	 */
	@Override
	public void configHandler(Handlers me) {
	    me.add(new HttpCacheHandler());
	    me.add(new ComboHandler());
	}
	
	/**
	 * 配置插件
	 */
	@Override
	public void configPlugin(Plugins me) {
		// 配置数据源
		MysqlDataSource ds = new MysqlDataSource();
		ds.setUrl(getProperty("jdbcUrl"));
	    ds.setUser(getProperty("user"));
	    ds.setPassword(getProperty("password"));
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(ds).setShowSql(true);
		arp.setContainerFactory(new CaseInsensitiveContainerFactory(false));
		me.add(arp);
		// 添加表匹配
		arp.addMapping("user_info", User.class);
		arp.addMapping("blog", Blog.class);
		arp.addMapping("wb_login", WBLogin.class);
		arp.addMapping("options", Options.class);
		arp.addMapping("links", Links.class);
		arp.addMapping("mail_verify", MailVerify.class);
		arp.addMapping("wx_rule", WxRule.class);
		// 添加BAE cache
		// 插件加载一直空指针
//		me.add(new MemCachePlugin());
//		me.add(new RedisPlugin());
	}
	
    /**
     * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     */
    public static void main(String[] args) {
        JFinal.start("WebContent", 8080, "/", 10);
    }
}
